package main

import (
	"encoding/json"
	"log"
	"math/rand"
	"time"
)

func main()  {
	// 执行抽单算法 range范围  num奖励数
	var drawScopeArr []int // 生成抽单范围
	var drawArr []int      // 抽单结果
	var otherDrawArr []int // 抽单结果
	var drawIndex int64
	for i := 1; i <= 10; i++ {
		drawScopeArr = append(drawScopeArr, i)
	}
	drawNumber := 20 // 抽单数
	currDrawNumber := 6

	if drawIndex == 1 || len(drawArr) == 0 {
		drawArr = regularDrawNumber(drawScopeArr, drawArr, otherDrawArr, drawNumber, currDrawNumber) // 1固定位置抽取
		log.Println("固定抽单的数组", drawArr)

		//drawArr = randomDrawNumber(drawScopeArr, drawArr, drawNumber) // 2轮次随机抽取
		//log.Println("随机抽单数组", drawArr)

		arr, _ := json.Marshal(drawArr)
		log.Println("测试测试", arr)
	}
}

// 固定抽单法
func regularDrawNumber(drawScopeArr []int, drawArr []int, otherDrawArr []int, drawNumber int, currDrawNumber int) []int {
	var index = 1
	if len(drawArr) == currDrawNumber {
		println("退出递归：", len(drawArr))
		return drawArr
	}
	mod := len(drawScopeArr) / drawNumber
	for i := 0; i < len(drawScopeArr); i++ {
		// mod 等于1取值法
		if i == mod*index-1 {
			drawArr = append(drawArr, drawScopeArr[i])
			if mod == 1 {
				index = index + 2
			} else {
				index = index + 1
				if index > drawNumber {
					index = 1
				}
			}

		} else {
			otherDrawArr = append(otherDrawArr, drawScopeArr[i])
		}
	}
	drawScopeArr = otherDrawArr
	otherDrawArr = []int{}
	drawNumber = currDrawNumber - len(drawArr)

	return regularDrawNumber(drawScopeArr, drawArr, otherDrawArr, drawNumber, currDrawNumber)
}

// 随机抽单法
func randomDrawNumber(drawScopeArr []int, drawArr []int, drawNumber int) []int {

	if len(drawArr) == drawNumber {
		println("退出递归：", len(drawArr))
		return drawArr
	}
	// 随机数
	rand.Seed(time.Now().UnixNano())

	// 随机抽取一单
	drawNum := drawScopeArr[rand.Intn(len(drawScopeArr))]

	//查重
	exist := false
	for _, v := range drawArr {
		if v == drawNum {
			exist = true
			break
		}
	}

	if !exist {
		drawArr = append(drawArr, drawNum)
	}

	//递归
	return randomDrawNumber(drawScopeArr, drawArr, drawNumber)
}
